using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

namespace SiteECSWeb
{   
    [DataObject]
    public class MStudyBLL
    {
        private MStudyTableAdapter _MStudyAdapter = null;

        protected MStudyTableAdapter MStudyAdapter
        {
            get
            {
                if (_MStudyAdapter == null)
                {
                    _MStudyAdapter = new MStudyTableAdapter();
                }
                return _MStudyAdapter;
            }

        }


        #region SelcetMStudy
        public int GetTotalNumberOfMStudies()
        {
            Nullable<int> totalNull =
                (Nullable<int>)MStudyAdapter.TotalNumberOfMStudy();
            return totalNull.GetValueOrDefault();
        }
        public int GetTotalNumberOfMStudies(string FilterStr)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return GetTotalNumberOfMStudies();
            }
            else
            {
                Nullable<int> totalNull = (Nullable<int>)MStudyAdapter.
                TotalNumberOfMStudyByFilterExp(FilterStr);
                return totalNull.GetValueOrDefault();
            }
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.MStudyDataTable GetAllMStudyData()
        {
            return MStudyAdapter.GetMstudyRowRankData();
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.MStudyDataTable GetMStudyByMStdyID(int Mstdy_id)
        {
            return MStudyAdapter.GetMstudyDataByMstdyId(Mstdy_id);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.MStudyDataTable GetMstudyByMstdyNmArFrstLetter(string Mstdy_name ,int TopCount)
        {
            return MStudyAdapter.
                GetMstudyDataByMstdyNmArFrstLetter(Mstdy_name ,TopCount);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.MStudyDataTable GetMStudyByRowRank(int RowRank)
        {
            return MStudyAdapter.GetMstudyDataByRowRank(RowRank);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.MStudyDataTable GetMStudyByCountryEN(String CuntryEn)
        {
            return MStudyAdapter.GetMstudyDataByCuntryEn(CuntryEn.Trim());
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.MStudyDataTable 
            GetMStudyByCityIDYearMonth(int cityId, int year, int month)
        {
            return MStudyAdapter.GetMstudyDataByCityIdYearMonth(cityId, year, month);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public PagedDataSource GetMStudyByCuntryEnAsPagedDataSource
            (string CuntryEn, int pageIndex, int pageSize)
        {
            EcsSiteDS.MStudyDataTable MStduyTable = GetMStudyByCountryEN(CuntryEn);
            
            PagedDataSource pagedData = new PagedDataSource();
            pagedData.DataSource = MStduyTable.Rows;
            pagedData.AllowPaging = true;
            pagedData.CurrentPageIndex = pageIndex;
            pagedData.PageSize = pageSize;
            return pagedData;
        }

       

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.MStudyDataTable GetMStudyAsGridPagesAndFilterExpression(int startRowIndex,
            int maximumRows, string sortExpression)
        {
            return MStudyAdapter.
                GetMStudyDataByGridPages(startRowIndex, maximumRows, sortExpression);
        }
       
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.MStudyDataTable GetMStudyAsGridPagesAndFilterExpression(int startRowIndex, int maximumRows,
            string FilterStr, string sortExpression)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return
                    GetMStudyAsGridPagesAndFilterExpression(startRowIndex, maximumRows, sortExpression);
            }
            else
            {
                return MStudyAdapter.GetMStudyDataByGridPagesFilterExp
                    (startRowIndex, maximumRows, FilterStr, sortExpression);
            }
        }
     
        #endregion

        #region InsertMStudy
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddMStudy(string Mstdy_name, DateTime Mstdy_date,
            DateTime? Mstdy_publish_date,
            string Mstdy_memo, bool Formemberonly, bool? Publishing, int City_id ,
            int? ProdCat_Id,string Mstdy_ImagePath,string Mstdy_Path)
        {

            EcsSiteDS.MStudyDataTable MStdyTable = new EcsSiteDS.MStudyDataTable();
            EcsSiteDS.MStudyRow MStdyRow = MStdyTable.NewMStudyRow();

            
            if (string.IsNullOrEmpty(Mstdy_memo)) MStdyRow.SetMstdy_memoNull(); else MStdyRow.Mstdy_memo = Mstdy_memo;
            if (Publishing == null) MStdyRow.SetMstdy_publish_dateNull(); else MStdyRow.Publishing = Publishing.Value;
            if (ProdCat_Id == null) MStdyRow.SetProdCat_IdNull(); else MStdyRow.ProdCat_Id = ProdCat_Id.Value;
            if (string.IsNullOrEmpty(Mstdy_ImagePath)) MStdyRow.SetMstdy_ImagePathNull(); else MStdyRow.Mstdy_ImagePath = Mstdy_ImagePath;
            if (string.IsNullOrEmpty(Mstdy_Path)) MStdyRow.SetMstdy_PathNull(); else MStdyRow.Mstdy_Path = Mstdy_Path;
            if (!Mstdy_publish_date.HasValue) MStdyRow.SetMstdy_publish_dateNull(); else MStdyRow.Mstdy_publish_date = Mstdy_publish_date.Value;

            MStdyRow.Mstdy_name = Mstdy_name;
            MStdyRow.Mstdy_date =Mstdy_date ;
            //MStdyRow.Mstdy_publish_date = Mstdy_publish_date.Value;
            MStdyRow.Formemberonly =Formemberonly;
            MStdyRow.City_id =City_id ;

            MStdyTable.AddMStudyRow(MStdyRow);
            
            int rowsAffected = MStudyAdapter.Update(MStdyTable);

            return (rowsAffected == 1) ? MStdyRow.Mstdy_id : -1;
        }

        #endregion

        #region UpdateMStudy

        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdateMStudy(string Mstdy_name, DateTime Mstdy_date,
            DateTime? Mstdy_publish_date,
            string Mstdy_memo, bool Formemberonly, bool? Publishing, int City_id, int? ProdCat_Id, 
            string Mstdy_ImagePath, string Mstdy_Path, int Mstdy_id)
        {

            EcsSiteDS.MStudyDataTable MStdyTable = MStudyAdapter.GetMstudyDataByMstdyId(Mstdy_id);
            if (MStdyTable.Count ==0)
            {
                return false;
            }

            EcsSiteDS.MStudyRow MStdyRow = MStdyTable[0];

            if (Mstdy_publish_date == null ) MStdyRow.SetMstdy_publish_dateNull(); else MStdyRow.Mstdy_publish_date = Mstdy_publish_date.Value;
            if (string.IsNullOrEmpty(Mstdy_memo) ) MStdyRow.SetMstdy_memoNull(); else MStdyRow.Mstdy_memo = Mstdy_memo;
            if (Publishing == null) MStdyRow.SetPublishingNull(); else MStdyRow.Publishing = Publishing.Value;
            if (Mstdy_publish_date == null) MStdyRow.SetMstdy_publish_dateNull(); else MStdyRow.Mstdy_publish_date = Mstdy_publish_date.Value;
            if (ProdCat_Id == null) MStdyRow.SetProdCat_IdNull(); else MStdyRow.ProdCat_Id = ProdCat_Id.Value;
            if (string.IsNullOrEmpty(Mstdy_ImagePath) ) MStdyRow.SetMstdy_ImagePathNull(); else MStdyRow.Mstdy_ImagePath = Mstdy_ImagePath;
            if (string.IsNullOrEmpty(Mstdy_Path )) MStdyRow.SetMstdy_PathNull(); else MStdyRow.Mstdy_Path = Mstdy_Path;

            MStdyRow.Mstdy_name = Mstdy_name;
            MStdyRow.Mstdy_date = Mstdy_date;
            MStdyRow.Formemberonly = Formemberonly;
            MStdyRow.City_id = City_id;
            

            int rowsAffected = MStudyAdapter.Update(MStdyRow);

            return rowsAffected == 1;
        }

        #endregion

        #region DeleteMstudy
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteMstduy(int Mstdy_id)
        {
            EcsSiteDS.MStudyDataTable MStdyTable = MStudyAdapter.GetMstudyDataByMstdyId(Mstdy_id);

            if (MStdyTable.Count == 0)
            {
                return false;
            }
            EcsSiteDS.MStudyRow MStdyRow = MStdyTable[0];

            if (!MStdyRow.IsMstdy_PathNull())
            {
                System.IO.File.Delete(HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                            + MStdyRow.Mstdy_Path));
            }
            if (!MStdyRow.IsMstdy_ImagePathNull())
            {
                System.IO.File.Delete(HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                           + MStdyRow.Mstdy_ImagePath));
            }
        
            int rowsAffected = MStudyAdapter.Delete(Mstdy_id);
            return rowsAffected == 1;
        }
        #endregion
    }
}
